﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InSafetyAirClient.GUI.Flight
{
    public partial class UpdatePM : UserControl
    {
        public UpdatePM()
        {
            InitializeComponent();
        }


        List<InSafetyAirClient.InSafetyAirService.PlaneModel> models = null;

        private void UpdatePM_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                populateList();
            }
        }

        private void populateList()
        {
            AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
            ServiceSingle servS = ServiceSingle.Instance;
            //
            //Search reg
            //
            models = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.PlaneModel).Name).OfType<InSafetyAirClient.InSafetyAirService.PlaneModel>().ToList();
            foreach (InSafetyAirService.PlaneModel planeM in models)
            {
                comboBox1.Items.Add(planeM.planeModelName);
                autoComplete.Add(planeM.planeModelName);
            }
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = autoComplete;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (InSafetyAirService.PlaneModel planeM in models)
            {
                if (planeM.planeModelName.Equals(comboBox1.Text))
                {
                    planeM.planeModelName = textBox1.Text;
                    planeM.seats = (short)System.Convert.ToInt32(textBox2.Text);
                    ServiceSingle.Instance.getService().UpdateToDatabase(planeM);
                    populateList();
                }
            }
        }
    }
}
